package com.myjavalab.collection;

import java.util.HashMap;
import java.util.Map;

import com.myjavalab.util.PrintUtil;
/**
 * the key of the HashMap must override hashCode() and equals(Object o)
 * @author Andy.Rong
 *
 */
public final class HashMapTest {

	
	public static void main(String[] args) {
//		testPhoneNumber();
		testLong();
	}
	
	public static void testLong() {
		Map<Long,Integer> map = new HashMap<Long,Integer>();
		map.put(new Long(1L), 1);
		System.out.println(map.get(1L));
		map.put(new Long(1L), 2);
		System.out.println(map.get(1L));
		map.remove(new Long(1L));
		System.out.println(map.get(1L));
	}
	public static void testLong2() {
		Map<Long,Integer> map = new HashMap<Long,Integer>();
		putLong(map, 1L);
		getLong(map);
	}
	public static void putLong(Map<Long,Integer> map, long num) {		
		map.put(num, 1);
	}
	public static void getLong(Map<Long,Integer> map) {
		System.out.println(map.get(1L));
	}
	
	public static void testPhoneNumber(Map<Long,Integer> map) {
		Map m = new HashMap();
		PhoneNumber p1 = new PhoneNumber(123456);
		PhoneNumber p2 = new PhoneNumber(123456);
		System.out.println(p1.hashCode());
		System.out.println(p2.hashCode());
		System.out.println(p1 == p2);
		System.out.println(p1.equals(p2));
		m.put(new PhoneNumber(123456), "Jenny");
		m.put(null, "null");
		m.put("", "kong");
		System.out.println(m.get(new PhoneNumber(123456)));
		System.out.println(m.get(null));
		System.out.println(m.get(""));
	}
}

class PhoneNumber {
	private final int number;

	public PhoneNumber(int number) {
		this.number = number;
	}

	public boolean equals(Object o) {
		if (o == this)
			return true;
		if (!(o instanceof PhoneNumber))
			return false;
		PhoneNumber pn = (PhoneNumber) o;
		return pn.number == number;
	}
	
	@Override
	public int hashCode() {
		return 1;
	}
}
